package com.example.Helper;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.widget.Toast;

import com.example.Util.CloseResult;
import com.example.Util.CommandProc;
import com.example.Util.DevComm;
import com.example.Util.FingerCallResult;
import com.example.Util.FingerCallback;
import com.example.Util.IFPListener;
import com.example.Util.LogAndToastUtils;
import com.example.Util.MessageUtil;

import java.util.Arrays;

public class FingerHelper {
    private final CommandProc commandProc;
    private final FingerCallback callback;
    private final Context context;
    private byte[] binImage;

    int templateSize = 0;
    byte[] templateData;
    public FingerHelper(FingerCallback callback, Activity activity) {
        this.callback = callback;
        this.context = activity.getApplicationContext();
        binImage = new byte[1024 * 100];
        templateData = new byte[DevComm.SZ_MAX_RECORD_SIZE];
        commandProc = new CommandProc();
        commandProc.setListener(new IFPListener.FPCommandListener() {
            @Override
            public void cmdProcReturn(int cmdCode, int retCode, int param1, int param2) {
                processingReturnResult(cmdCode, retCode, param1, param2);
            }

            @Override
            public void cmdProcRunning(Runnable procRspPacket) {
                activity.runOnUiThread(procRspPacket);
            }

            @Override
            public void cmdImageProcReturn(byte[] pData, int pSize) { // command process data result获取返回数据
                if (pSize > DevComm.SZ_MAX_RECORD_SIZE) {
                    System.arraycopy(pData, 0, binImage, 0, pSize);
                    callback.imageDataResult(binImage);
                } else {
                    System.arraycopy(pData, 0, templateData, 0, pSize);
                    templateSize = pSize;
                    callback.imageDataResult(templateData);

                }
            }
        }, new IFPListener.FPCancelListener() {

            @Override
            public void cancelReturn(int retCode, boolean result, int cmdCode) {
                FingerCallResult cancelResult = new FingerCallResult();
                cancelResult.setRetCode(retCode);
                cancelResult.setCmdCode(cmdCode);
                cancelResult.setResult(true);
                String message;
                if (result) {
                    message = "中断成功";
                } else {
                    message = "中断失败";
                }
                cancelResult.setMessage(message);
                callback.onResult(cancelResult);

            }

            @Override
            public void cancelRunning(Runnable runnable) {
                activity.runOnUiThread(runnable);

            }
        });
    }

    private void processingReturnResult(int cmdCode, int retCode, int param1, int param2) {
        String message = null;
        switch (cmdCode) {
            case (short) DevComm.CMD_ENROLL_CODE:
            case (short) DevComm.CMD_VERIFY_CODE:
            case (short) DevComm.CMD_IDENTIFY_CODE:
            case (short) DevComm.CMD_FINGER_DETECT_CODE:
            case (short) DevComm.CMD_CHANGE_TEMPLATE_CODE: {
                message = switchParam1(retCode, param1, param2, cmdCode);
                break;
            }
            case (short) DevComm.CMD_CLEAR_TEMPLATE_CODE:
            case (short) DevComm.CMD_CLEAR_ALLTEMPLATE_CODE:
            case (short) DevComm.CMD_FP_CANCEL_CODE:
            case (short) DevComm.CMD_UP_IMAGE_CODE:
            case (short) DevComm.CMD_FEATURE_OF_CAPTURED_FP_CODE:
                message = switchCode(cmdCode, retCode, param1);
                break;
            case (short) DevComm.CMD_WRITE_TEMPLATE_CODE:
                message = switchCode(cmdCode, retCode, param1) + " 编号为:" + param1;
                break;
            default:
                break;
        }

        if (message != null && !message.isEmpty()) {
            FingerCallResult fingerUtilsCallBackResult = new FingerCallResult();
            fingerUtilsCallBackResult.setResParam1(param1);
            fingerUtilsCallBackResult.setCmdCode(cmdCode);
            fingerUtilsCallBackResult.setRetCode(retCode);
            fingerUtilsCallBackResult.setMessage(message);
            if (retCode == (short) DevComm.ERR_SUCCESS) {
                fingerUtilsCallBackResult.setResult(true);
            } else {
                fingerUtilsCallBackResult.setResult(false);
            }
            callback.onResult(fingerUtilsCallBackResult);
        }


    }

    @SuppressLint("DefaultLocale")
    private String switchCode(int p_nCode, int p_nRet, int p_nParam1) {
        String m_strPost;
        if (p_nRet == (short) DevComm.ERR_SUCCESS) {
            m_strPost = MessageUtil.getInstance().getCodeMessage((short) p_nCode);
        } else {
            m_strPost = MessageUtil.getInstance().GetErrorMsg((short) p_nParam1);
        }
        return m_strPost;
    }

    @SuppressLint("DefaultLocale")
    private String switchParam1(int p_nRet, int p_nParam1, int p_nParam2, int p_nCode) {
        String m_strPost;
        if (p_nRet == (short) DevComm.ERR_SUCCESS) {
            if (!MessageUtil.getInstance().getCodeMessage((short) p_nParam1).isEmpty()) {
                m_strPost = MessageUtil.getInstance().getCodeMessage((short) p_nParam1);
            } else {
                m_strPost = "操作成功编号为: " + p_nParam1;
            }

        } else {
            m_strPost = MessageUtil.getInstance().GetErrorMsg((short) p_nParam1);
            if ((byte) ((short) p_nParam1 & 0xFF) == DevComm.ERR_BAD_QUALITY) {
                m_strPost += "再次按压... ";
            } else {
                if (p_nParam1 == DevComm.ERR_DUPLICATION_ID) {
                    m_strPost += String.format(" %d.", p_nParam2);
                }
            }
        }
        return m_strPost;

    }


    public boolean checkId(int id) {
        if (id > (DevComm.MAX_RECORD_COUNT) || id < 1) {
            Toast.makeText(context,"请输入正确的ID范围(1~" + (short) DevComm.MAX_RECORD_COUNT + ")",Toast.LENGTH_SHORT).show();
            return false;
        } else {
            return true;
        }
    }

    public boolean openDevice(Context context) {
        return commandProc.openDevice(context);
    }

    public CloseResult closeDevice() {
        return commandProc.closeDevice();
    }

    public void identifyFinger(final boolean force) {
        commandProc.identify(force);
    }

    public void enrollFinger(int id, boolean force) {
        if(!checkId(id)){
            return;
        }
        commandProc.enRollTemplate(id, force);
    }

    public void deleteAllFinger(boolean force) {
        commandProc.deleteAll(force);
    }

    public void cancel() {
        commandProc.cancel();
    }

    public void getFingerImageData(){
        commandProc.getFingerImageData(false);
    }
    public void getFingerTemplate(){
        commandProc.getFingerTemplate(false);
    }

    public void inputTemplate(int no,byte[] templateData){
        if(!checkId(no)){
            return;
        }
        byte[] w_pTemplate = new byte[DevComm.SZ_TEMPLATE_SIZE];
        int w_nTemplateSize = DevComm.SZ_TEMPLATE_SIZE;
        System.arraycopy(templateData,0,w_pTemplate,0,w_nTemplateSize);
        LogAndToastUtils.log(Arrays.toString(templateData));
        commandProc.writeTemplate(no, w_pTemplate, w_nTemplateSize, false);
    }
}
